using System;
using System.Web.UI.WebControls;

using Impact.Web.Foundation;
using Impact.Controllers;
using Impact.Model.Objects;
using Impact.Infrastructure.Configuration;
using Impact.Infrastructure;
using StructureMap;

namespace Impact.Web.Admin
{
    /// <summary>
    /// code-behind for Template listing page
    /// </summary>
    public partial class Templates : PageBase
    {
        private PageController controller;

        /// <summary>
        /// override for onload to perform startup processing
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            controller = new PageController(UserId);

            base.OnLoad(e);

        }

        /// <summary>
        /// overridden to handling binding on this page
        /// </summary>
        protected override void Bind()
        {
            TemplateCollection pages = controller.GetTemplates();
            ImpactSettings appSettings = ObjectFactory.GetInstance<IConfigProvider>().Settings;
            if (pages.Count > 0)
            {
                templateList.DataSource = pages;
                templateList.DataBind();

                categoryTemplate.DataSource = pages;
                categoryTemplate.DataTextField = Template.Columns.Name;
                categoryTemplate.DataValueField = Template.Columns.TemplateID;
                categoryTemplate.DataBind();
                categoryTemplate.SelectedValue = appSettings.CategoryTemplate;

                articleTemplate.DataSource = pages;
                articleTemplate.DataTextField = Template.Columns.Name;
                articleTemplate.DataValueField = Template.Columns.TemplateID;
                articleTemplate.DataBind();
                articleTemplate.SelectedValue = appSettings.ArticleTemplate;

                tagTemplate.DataSource = pages;
                tagTemplate.DataTextField = Template.Columns.Name;
                tagTemplate.DataValueField = Template.Columns.TemplateID;
                tagTemplate.DataBind();
                tagTemplate.SelectedValue = appSettings.TagTemplate;

            }
            else
            {
                templateList.Visible = false;
                noTemplatesPanel.Visible = true;
                categoryTemplate.Enabled = false;
                articleTemplate.Enabled = false;
                tagTemplate.Enabled = false;
            }
        }


        /// <summary>
        /// event handler for when a button is clicked in the template listing repeater
        /// </summary>
        protected void TemplateList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int pageId = Int32.Parse(e.CommandArgument.ToString());
            switch (e.CommandName)
            {
                case "delete":
                    controller.DeletePage(pageId);
                    break;

                default:
                    throw new ArgumentException(string.Format("Command not found: {0}", e.CommandName));
            }
            Bind();
        }


        /// <summary>
        /// event handler fired when the user changes one of the template config options
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TemplateChanged(object sender, EventArgs e)
        {
            DropDownList list = sender as DropDownList;

            if (list.SelectedValue != "-1")
            {
                ImpactSettings appSettings = ObjectFactory.GetInstance<IConfigProvider>().Settings;
                ImpactSettings newSettings = (ImpactSettings)appSettings.Clone();



                if (list.Equals(categoryTemplate))
                {
                    newSettings.CategoryTemplate = list.SelectedItem.Text;
                }
                else if (list.Equals(articleTemplate))
                {
                    newSettings.ArticleTemplate = list.SelectedItem.Text;
                }
                else if (list.Equals(tagTemplate))
                {
                    newSettings.TagTemplate = list.SelectedItem.Text;
                }

                ObjectFactory.GetInstance<IConfigProvider>().Settings = newSettings;
            }

        }


    }
}
